package jgl.vajra.management.widget.recyclerView.pagemanagestrategy;


import jgl.vajra.management.widget.mvvm.view.AppActivityManager;
import jgl.vajra.management.widget.pagemanage.PageManager;

import io.reactivex.disposables.Disposable;

/**
 * @Author zhangshuqi
 * @CreateTime 2018/3/19
 * @Describe
 */

public class PageManagerStrategy extends LoadingManageStrategy {

    private PageManager mPageManager;

    public PageManagerStrategy(PageManageBuilder builder) {
        super(builder);
    }


    @Override
    public void showPageContent() {
        if (mPageManager != null) mPageManager.showContent();
    }

    @Override
    public void showPageLoading(String loadingMsg, Disposable d) {
        if (mPageManager == null) {
            mPageManager = PageManager.init(mBuilder.getRecyclerView().getParent(), mBuilder.getRetryMsg(), mBuilder.getEmptyMsg(), loadingMsg, new Runnable() {
                @Override
                public void run() {
                    if (mPageErrorRetryListener != null)
                        mPageErrorRetryListener.errorRetry();
                }
            });
            if (mBuilder.isShowPageLayoutTop()) {
                mPageManager.showLayoutTopMargin();
            }
            if (mBuilder.getPageManagerBackgroundRes() !=-1)
                mPageManager.setPageManagerBackground(mBuilder.getPageManagerBackgroundRes());
        }
        if (mPageManager != null) {
            if (mBuilder.isShowNoNet() && !PageManager.isNetWorkAvailable(mBuilder.getContext())) {
                if (d != null && !d.isDisposed()) {
                    d.dispose();
                }
                PageManager.showNoNetWorkDlg(AppActivityManager.getAppActivityManager().currentActivity());
                mPageManager.showError(mBuilder.getRetryMsg());
            } else {
                mPageManager.showLoading(mBuilder.getLoadingMsg());
            }
        }


    }

    @Override
    public void showPageEmpty(String emptyMsg) {
        if (mPageManager != null) mPageManager.showEmpty(emptyMsg, mBuilder.getEmptyImageRes(),mBuilder.getShowEmptyLoginButton());
    }

    @Override
    public void showPageError(String errorMsg) {
        if (mPageManager != null) mPageManager.showError(errorMsg, mBuilder.getErrorImageRes());
    }


}
